package com.yanqu.road.server.cmd.union;

import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.utils.UUIDHelper;
import com.yanqu.road.utils.string.StringUtils;

//@Cmd(code = Protocol.S_UNION_CREATE, desc = "创建联盟")
public class CreateUnionCmd extends AbstractCommand {

	private String unionName;

	private int unionFlag;

	private boolean isAllowRandomJoin;

	private String outNotice;

	private String qqCode;

	private String wechatCode;

	private String innerNotice;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		UnionProto.UnionCreateReqMsg reqMsg = UnionProto.UnionCreateReqMsg.parseFrom(packet.getBytes());
		unionName = reqMsg.getUnionName();
		unionFlag = reqMsg.getUnionFlag();
		outNotice = reqMsg.getOutNotice();
		qqCode = reqMsg.getQqCode();
		wechatCode = reqMsg.getWechatCode();
		isAllowRandomJoin = reqMsg.getIsAllowRandomJoin();
		innerNotice = reqMsg.getInnerNotice();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		if(!SystemOpenMgr.systemOpen(player, eSystemId.Union.getValue())){
			ret = GameErrorCode.E_SYSTEM_NO_OPEN;
			return;
		}

		if (!StringUtils.isNullOrEmpty(player.getUserInfo().getUnionUid())) {//判断有没有联盟
			ret = GameErrorCode.E_UNION_HAS_JOIN_UNION;
			return;
		}

		if(VipMgr.getVipLv(player.getUserInfo().getVipExp()) < GameConfig.UNION_CREATE_NEED_VIP_LEVEL){
			ret = GameErrorCode.E_VIP_LEVEL_NO_ENOUGH;
			return;
		}

		if(!ConfigMgr.hasUnionFlag(unionFlag)){
			ret = GameErrorCode.E_UNION_FLAG_NO_FOUND;
			return;
		}

		UnionMember unionMember = UnionMgr.getUnionMember("", player.getUserId());
		if(null != unionMember){
			long nowTime = System.currentTimeMillis() / 1000;
			if(nowTime - unionMember.getQuitTime() < GameConfig.UNION_NEXT_JOIN_NEED_TIME){
				ret = GameErrorCode.E_UNION_NEXT_JOIN_TIME_NO_ENOUGH;
				return;
			}
		}
		String unionUid = UUIDHelper.randomUUID(String.valueOf(GameServer.getInstance().getServerId()));
		ret = UnionConfigMgr.checkUnionName(unionName, unionUid, player);
		if(0 != ret){
			return;
		}
		ret = UnionConfigMgr.checkOutNotice(outNotice, unionUid, player);
		if(0 != ret){
			return;
		}
		ret = UnionConfigMgr.checkInnerNotice(innerNotice);
		if(0 != ret){
			return;
		}
		//创建商会不需要qq和微信
		qqCode = "";
		wechatCode = "";

		// 创建联盟
		ret = UnionMgr.createUnion(player, unionName, isAllowRandomJoin, outNotice, wechatCode, qqCode, innerNotice, unionFlag, unionUid);

		//增加了实时检测，不上报了
		//创建成功上报
//		if (ret == 0) {
//			ThirdPartyMgr.unionNameReport(player.getUserInfo().getUnionUid(), unionName);
//			ThirdPartyMgr.unionNoticeReport(player.getUserInfo().getUnionUid(), outNotice);
//		}
	}



	@Override
	public void sendData(GamePlayer player) {
		UnionProto.UnionCreateRespMsg.Builder respMsg = UnionProto.UnionCreateRespMsg.newBuilder();
		respMsg.setRet(ret);
		player.sendPacket(Protocol.U_UNION_CREATE, respMsg);
	}
}
